package com.smithlibraties.util.annotation.register.render;

import com.smithlibraties.SmithLibraries;
import com.smithlibraties.util.annotation.AnnotationClassGetter;
import com.smithlibraties.util.annotation.register.RegisterTileEntity;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;

import java.util.ArrayList;
import java.util.List;

public class RegisterTileEntityRender {
    List<TileEntityRenderAttribute> list = new ArrayList<>();

    public RegisterTileEntityRender() {
        List<Class> tileEntityRenderCollections = AnnotationClassGetter.getAllClassByInterface(SmithLibraries.getAllClasses(), RegisterTileEntity.Render.class);//获取实现RegisterClass注释的类的List
        for (Class clazz : tileEntityRenderCollections) {//开始集中继承IRegister接口的的类的属性的List
            list = (RegisterTileEntityRender.getAnnotationList(clazz));
        }
        for (TileEntityRenderAttribute registerAttribute : list) {
            try {
                registerTileEntity(registerAttribute.getClazz(), registerAttribute.getM().newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    private static void registerTileEntity(Class<? extends TileEntity> tileEntityClass, Object specialRenderer) {
        ClientRegistry.bindTileEntitySpecialRenderer(tileEntityClass, (TileEntitySpecialRenderer) specialRenderer);
    }

    @SideOnly(Side.CLIENT)
    private static List<TileEntityRenderAttribute> getAnnotationList(Class clazz) {
        List<TileEntityRenderAttribute> list1 = new ArrayList<>();
        if (clazz.isAnnotationPresent(RegisterTileEntity.Render.class)) {
            RegisterTileEntity.Render registerTileEntityRender = (RegisterTileEntity.Render) clazz.getAnnotation(RegisterTileEntity.Render.class);
            list1.add(new TileEntityRenderAttribute(clazz, registerTileEntityRender.clazz()));
        }
        return list1;
    }
}
